﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using COM.Data;
using COM.Service;
using COM.Web;
using WebApp.Data;
using WebApp.Service;
using WebApp.Domain;
using System.Xml.Linq;

namespace WebApp.Web.Service
{
    public interface IAppUserPrincipalFactory : IUserPrincipalFactory
    {
    }

    public class AppUserPrincipalFactory : UserPrincipalFactory, IAppUserPrincipalFactory
    {
        private IPermissionRoleListService permissionRoleListService;
        public AppUserPrincipalFactory(IPermissionRoleListService permissionRoleListService)
            : base(permissionRoleListService)
        {
            this.permissionRoleListService = permissionRoleListService;
        }

        public override IPermissionUserPrincipal CreateMvcUserPrincipal(Guid userID, string userCode)
        {
            if (userCode == MvcUserCookie.AnonymousUserCode)
            {
                var roleItemList = new List<PermissionRole>() { new TempFileWebAnonymous(userID, userCode), WebAnonymousPermissionRole.Instance };
                var user = new PermissionUserPrincipal(userID, userCode, userCode, roleItemList);
                return user;
            }
            else
            {
                return this.CreateTempFileUserPrincipal(userID, userCode, userCode);
            }
        }

        public IPermissionUserPrincipal CreateTempFileUserPrincipal(Guid userID, string userCode, string userName)
        {
            //权限系统不依赖于数据库服务,否则可能在IRuntimeService集成时会循环引用
            var userService = Unity.Instance.GetService<IUserService>();
            var roleItemList = new List<PermissionRole>() { new TempFileUser(userID, userCode), WebUserPermissionRole.Instance, new WebApp.Data.AppEveryOne(userID, userCode) };
            var user = new PermissionUserPrincipal(userID, userCode, userName, roleItemList);
            return user;
        }

    }
}